package cc.rengu.igas.cops.common.dao.impl;

import cc.rengu.igas.cops.common.dao.CopsPayOrderInfoMapper;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.entity.PayOrderInfo;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.dbs.Database;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description 支付订单表操作实现
 * @Author panky
 * @date 2020/5/16
 **/
public class CopsPayOrderInfoMapperImpl implements CopsPayOrderInfoMapper {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppParamConstant.OLTP_POOL_NAME);

    @Override
    public List<PayOrderInfo> selectPayOrderInfoByMchntNo(String instId, String mchntNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppParamConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "copsSelectPayOrderInfoByMchntNo");

        PayOrderInfo payOrderInfo = new PayOrderInfo();
        payOrderInfo.setInstId(instId);
        payOrderInfo.setMchntNo(mchntNo);

        List<Object> objects = dbsUtil.dbsFetchLimitListData(sqlStr, payOrderInfo, PayOrderInfo.class);
        return objects.stream().map(item -> (PayOrderInfo) item).collect(Collectors.toList());
    }

    @Override
    public List<PayOrderInfo> selectPayOrderInfoByOrderId(String instId, String mchntOrderId, String sysSeqNum) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppParamConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "copsSelectPayOrderInfoByOrderId");
        PayOrderInfo payOrderInfo = new PayOrderInfo();
        payOrderInfo.setInstId(instId);
        payOrderInfo.setMchntOrderId(mchntOrderId);
        payOrderInfo.setSysSeqNum(sysSeqNum);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, payOrderInfo, PayOrderInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据订单号查询支付订单信息失败,instId:<{}>,sysSeqNum:<{}>", instId, mchntOrderId);
            return null;
        }
        return objectList.stream().map(item -> (PayOrderInfo) item).collect(Collectors.toList());
    }

    @Override
    public PayOrderInfo selectPayOrderInfoBySysSeqNum(String instId, String sysSeqNum) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppParamConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "copsSelectPayOrderInfoBySysSeqNum");
        PayOrderInfo payOrderInfo = new PayOrderInfo();
        payOrderInfo.setInstId(instId);
        payOrderInfo.setSysSeqNum(sysSeqNum);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, payOrderInfo, payOrderInfo);
        if (0 != iReturnCode) {
            rglog.error("根据订单流水号查询支付订单信息失败,instId:<{}>,sysSeqNum:<{}>", instId, sysSeqNum);
            return null;
        }
        return payOrderInfo;
    }

    @Override
    public List<PayOrderInfo> copsSelectPayOrderInfoByChannelReconKey(String instId, String sysSeqNum) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppParamConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "copsSelectPayOrderInfoByChannelReconKey");
        PayOrderInfo payOrderInfo = new PayOrderInfo();
        payOrderInfo.setInstId(instId);
        payOrderInfo.setSysSeqNum(sysSeqNum);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, payOrderInfo, PayOrderInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据订单号查询支付订单信息失败,instId:<{}>,sysSeqNum:<{}>", instId, sysSeqNum);
            return null;
        }
        return objectList.stream().map(item -> (PayOrderInfo) item).collect(Collectors.toList());
    }

    @Override
    public List<PayOrderInfo> selectPayOrderInfoByInstIdTxnDateMchntOrderId(String instId, String txnDate, String mchntOrderId) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppParamConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "copsSelectPayOrderInfoByInstIdTxnDateMchntOrderId");
        PayOrderInfo payOrderInfo = new PayOrderInfo();
        payOrderInfo.setInstId(instId);
        payOrderInfo.setMchntOrderId(mchntOrderId);
        payOrderInfo.setTxnDate(txnDate);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, payOrderInfo, PayOrderInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据订单号查询支付订单信息失败,instId:<{}>,sysSeqNum:<{}>", instId, mchntOrderId);
            return null;
        }
        return objectList.stream().map(item -> (PayOrderInfo) item).collect(Collectors.toList());
    }

    @Override
    public int copsUpdatePayOrderInfoByPrimaryKeyWithLock(PayOrderInfo payOrderInfo) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        if (0 != dbsUtil.dbsBeginTransaction()) {
            rglog.error("copsUpdatePayOrderInfoByPrimaryKeyWithLock开启事务失败！");
            return 1;
        }
        String selStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "copsSelectPayOrderInfoByPrimaryKeyForUpdate");
        PayOrderInfo payOrderInfo1 = new PayOrderInfo();
        int iReturnCode = dbsUtil.dbsExecuteSql(selStr, payOrderInfo, payOrderInfo1);
        if (0 != iReturnCode) {
            rglog.error("copsUpdatePayOrderInfoByPrimaryKeyWithLock, 按主键查询原订单失败！");
            dbsUtil.dbsEndTransaction(false);
            return iReturnCode;
        }
        String updStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "copsUpdatePayOrderInfoByPrimaryKey");
        iReturnCode = dbsUtil.dbsExecuteTransactionSql(updStr, payOrderInfo, null);
        if (0 != iReturnCode) {
            rglog.error("copsUpdatePayOrderInfoByPrimaryKeyWithLock, 按主键更新原订单失败！");
            dbsUtil.dbsEndTransaction(false);
            return iReturnCode;
        }
        dbsUtil.dbsEndTransaction(true);
        return 0;
    }

    @Override
    public int copsUpdatePayOrderInfoByPrimaryKeyAndOrderSts(PayOrderInfo payOrderInfo, String transStatus, String bizStatus) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String updStrTmp = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "copsUpdatePayOrderInfoByPrimaryKey");
        String updStr = updStrTmp + "AND TRANS_STATUS = \'" + transStatus + "\' AND BIZ_STATUS = \'" + bizStatus + "\'";
        PayOrderInfo payOrderInfo1 = new PayOrderInfo();
        int iReturnCode = dbsUtil.dbsExecuteSql(updStr, payOrderInfo, payOrderInfo1);
        if (0 != iReturnCode) {
            rglog.error("copsUpdatePayOrderInfoByPrimaryKey, 按主键更新原订单失败！");
            if (Database.DBS_FETCH_NULL == iReturnCode || Database.DBS_NOT_FOUND == iReturnCode) {
                rglog.error("copsUpdatePayOrderInfoByPrimaryKey按主键更新原订单失败原因：没有满足where语句的行");
            }
            return iReturnCode;
        }
        return 0;
    }
}
